package com.lagou.registry;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Zk连接持有对象，启动时自动连接zk服务器
 */
public class ZkClientHolder {

    public static CuratorFramework client = null;
//    public static ZkClient client = null;

    private static String address;

    private static int port;

    static{
        try {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
            Properties properties = new Properties();
            properties.load(is);
            RetryPolicy retryPolicy = new RetryOneTime(100);
            client = CuratorFrameworkFactory.newClient(properties.getProperty("zk.address")+":"+properties.getProperty("zk.port"),retryPolicy);
            client.start();
            System.out.println("zk启动完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}
